package com.zghhost.codedemo.windows;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class ProcessDetailWindow {
    public static void main(String[] args){
        final JFrame frame = new JFrame("查看Windows进程详情");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocation(100, 100);

        frame.setLayout(new BorderLayout());

        //上面
        final JPanel top = new JPanel();
        top.setPreferredSize(new Dimension(900, 40));
        top.setLayout(null);

        final JLabel userLabel = new JLabel("关键字:");
        userLabel.setBounds(5,5,60,30);
        top.add(userLabel);

        final JTextField likeKey = new JTextField();
        likeKey.setBounds(65,5,700,30);
        top.add(likeKey);

        final JButton actionBtn = new JButton();
        actionBtn.setBounds(765,5,80,29);
        actionBtn.setText("查看");
        top.add(actionBtn);




        //下面
        final JPanel down = new JPanel();
        down.setPreferredSize(new Dimension(900, 500));
        down.setLayout(new BorderLayout());

        actionBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                if(down.getComponentCount() > 0){
                    down.remove(down.getComponent(0));
                }


//              JOptionPane.showMessageDialog(null, likeKey.getText(), "提示",JOptionPane.DEFAULT_OPTION);
                String keyword = likeKey.getText();
                String commandStr = "wmic process "+ ("".equals(keyword) ? "" : "where \"commandline like '%"+keyword+"%'\"") +" get processid,name,commandline /format:csv";


                try{
                    java.util.List<Object[]> rowData = new ArrayList<Object[]>();
                    Process p = Runtime.getRuntime().exec(commandStr);
                    BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));
                    String line = null;
                    while((line = br.readLine()) != null){
                        try{
                            String[] lineArr = line.split(",");
                            rowData.add(new Object[]{lineArr[2],lineArr[3],lineArr[1]});
                        }catch(Exception e1){

                        }
                    }

                    Object[] columnNames = {"Name","Process ID", "Command Line"};
                    Object[][] o1 = new Object[rowData.size()][3];
                    int i = 0;
                    for(Object[] o : rowData){
                        o1[i++] = o;
                    }
                    JTable table = new JTable(o1, columnNames);

                    down.add(table.getTableHeader(), BorderLayout.NORTH);
                    down.add(table, BorderLayout.CENTER);
                }catch(Exception e1){
                    e1.printStackTrace();
                    Object[] columnNames = {"Name","Process ID", "Command Line"};
                    Object[][] rowData = {{"","",""}};
                    JTable table = new JTable(rowData, columnNames);

                    down.add(table.getTableHeader(), BorderLayout.NORTH);
                    down.add(table, BorderLayout.CENTER);
                }


                down.revalidate();
            }
        });



        frame.add(top, BorderLayout.NORTH);
        frame.add(down, BorderLayout.SOUTH);
        frame.setVisible(true);
        frame.pack();
    }
}
